<?php
/**
 * @file applist.php $
 * @author LinLin (linlin@haibao.com) $
 * @date 2015年9月7日 下午14:47:50 $
 * @brief  App所有版本列表 $
 */
namespace haibao\app\business;

class AppList extends \haibao\app\business\Base {
	
	/**
	 * @var $_instance
	 */
	private static $_instance;
	
	/**
	 * instance()
	 */
	private static function instance(){
		if(!self::$_instance){
			self::$_instance = new \haibao\app\data\AppList();
		}
		return self::$_instance;
	}
	
	/**
	 * 获取所有App版本
	 * @param unknown $query
	 */
	public static function getAppList($query){
		return self::instance()->getAll($query);
	}
	
	/**
	 * 添加App应用版本
	 */
	public static function addVersion($model){
		self::checkOperation(self::FUNC_CODE_APP_SPREAD_ADD);
		self::checkVersionModel($model);
		self::instance()->addVersion($model);
		self::saveOperateHistory(self::FUNC_CODE_APP_SPREAD_ADD, $model->toArray(true,true));
	}
	
	/**
	 * 删除App应用版本数据
	 */
	public static function updateVerStatus($id){
		self::checkOperation(self::FUNC_CODE_APP_SPREAD_DELETE);
		self::instance()->updateVerStatus($id);
		$arrSet['Id'] = $id;
		$arrSet['Status'] = \haibao\app\model\data\AppList::STATUS_DISABLE;
		self::saveOperateHistory(self::FUNC_CODE_APP_SPREAD_DELETE,$arrSet);
	}
	
	/**
	 * 根据APP版本号获取最高版本url地址
	 */
	public static function getAppUrlBySystem($system = \haibao\app\model\data\Promotion::DOWNLOADTYPE_ANDROID){
	    return self::instance()->getAppUrlBySystem($system);
	}
	
	/**
	 * 验证新增App应用版本
	 */
	private static function checkVersionModel($model){
		if(!$model->AppType){
			throw new BusinessException('应用类型不能为空');
		}
		if($model->AppVersion == ''){
			throw new BusinessException('应用版本不能为空');
		}
		if($model->Url == ''){
			throw new BusinessException('应用链接地址不能为空');
		}
		$result = self::instance()->getAppVersion($model->AppVersion,$model->AppType);
		if(!empty($result)){
			throw new BusinessException('应用版本重复');
		}
	}
	
	public static function getAppVersion($appVersion,$system){
		return self::instance()->getAppVersion($appVersion,$system);
	}
}
